package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        //1.保存套餐数据 到 setmeal 表中
        //新增完成够，套餐id 就存在了
        setmealService.save(setmealDto);

        //2.保存套餐菜品表   setmealDishes
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //2.1 为每个套餐菜品设置套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //2.2 写入到数据库
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //1.分页查询
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        //2.如果又条件就条件查询
        LambdaQueryWrapper<Setmeal> wapper = new LambdaQueryWrapper<>();
        if(name != null && name != ""){
            wapper.like(Setmeal::getName,name);
        }
        wapper.eq(Setmeal::getIsDeleted,0);
        Page<Setmeal> page2 = this.page(pageInfo, wapper);

        //再次封装CATEGORYName 属性
        //获得所有的套餐信息
        List<Setmeal> setmeals = page2.getRecords();
        List<SetmealDto>  setmealDtos = new ArrayList<>();
        //遍历所有的套餐对象，转变程一个套餐Dto对象，并且查询出分类名称
        for (Setmeal setmeal : setmeals) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            //查询分类名字
            Category category = categoryService.getById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtos.add(setmealDto);
        }
        //3.返回查询到的集合
        Page<SetmealDto> dtoPage = new Page<>();
        dtoPage.setRecords(setmealDtos);//数据集合
        dtoPage.setTotal(page2.getTotal());//设置总条数

        return dtoPage;
    }

    @Override
    public void deleteIds(Long[] ids) {
        //1.修改所有ids的is_delete 字段为1  代表了已经被逻辑删除
        //1.1查询出所有ids的套餐信息
        List<Setmeal> setmeals = this.listByIds(Arrays.asList(ids));
        //1.2 循环修改所有套餐的is_delete属性
        setmeals = setmeals.stream().map(c->{c.setIsDeleted(1); return c;}).collect(Collectors.toList());
        //1.3只需要修改
        this.updateBatchById(setmeals);

        //2.1 修改套餐菜品信息
        for (Setmeal setmeal : setmeals) {
            //查询出套餐的菜品信息
            LambdaQueryWrapper<SetmealDish> wapper = new LambdaQueryWrapper<>();
            wapper.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishService.list(wapper);
            setmealDishList = setmealDishList.stream().map(c->{
                c.setIsDeleted(1);
                return c;
            }).collect(Collectors.toList());
            //更新套餐菜品数据
            setmealDishService.updateBatchById(setmealDishList);
        }
        //执行成功
    }

    @Override
    public List<Setmeal> findList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wapper = new LambdaQueryWrapper<>();
        if(setmeal.getCategoryId() != null){
            wapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        }
        wapper.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(wapper);
        return list;
    }

    @Autowired
    private CategoryService categoryService;
}